package com.tensquare.notice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.notice.pojo.TbNotice;
import com.tensquare.notice.pojo.TbNoticeFresh;
import com.tensquare.notice.service.TbNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 文章(TbNotice)表控制层
 *
 * @author long
 * @since 2021-07-17 16:11:23
 */
@RestController
@RequestMapping("notice")
@CrossOrigin
public class TbNoticeController {
    /**
     * 服务对象
     */
    @Autowired
    private TbNoticeService tbNoticeService;

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        TbNotice notice = tbNoticeService.findById(id);
        return new Result(true, "查询成功", StatusCode.OK, notice);
    }

    @RequestMapping(value = "search/{page}/{size}", method = RequestMethod.POST)
    public Result findByPage(@RequestBody Map<String, Object> map,
                             @PathVariable Integer page,
                             @PathVariable Integer size) {
        Page<TbNotice> noticePage = tbNoticeService.findByPage(map, page, size);
        PageResult<TbNotice> result = new PageResult<>();
        result.setTotal((long) noticePage.getSize());
        result.setRows(noticePage.getRecords());
        return new Result(true, "查询成功", StatusCode.OK, result);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody TbNotice notice) {
        tbNoticeService.insert(notice);
        return new Result(true,"添加成功",StatusCode.OK);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        tbNoticeService.delete(id);
        return new Result(true,"删除成功",StatusCode.OK);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Result update(@RequestBody TbNotice notice){
        tbNoticeService.update(notice);
        return new Result(true,"更新成功",StatusCode.OK);
    }

    /**
     * 根据用户Id查询该用户待推送的消息
     */
    @RequestMapping(value = "fresh/{userId}/{page}/{size}",method = RequestMethod.GET)
    public Result freshPage(@PathVariable String userId,
                            @PathVariable Integer page,
                            @PathVariable Integer size){
        Page<TbNoticeFresh> freshByPage = tbNoticeService.findFreshByPage(userId, page, size);
        PageResult<TbNoticeFresh> result = new PageResult<>();
        result.setRows(freshByPage.getRecords());
        result.setTotal((long) freshByPage.getSize());
        return new Result(true,"查询成功",StatusCode.OK,result);
    }

    /**
     * 删除待推送消息
     */
    @RequestMapping(value = "/fresh/{id}",method = RequestMethod.DELETE)
    public Result freshDelete(@PathVariable String id){
        tbNoticeService.freshDelete(id);
        return new Result(true,"删除成功",StatusCode.OK);
    }
}
